var express = require('express');
var mongoose = require('mongoose');
var articleModel = require('../model/articleModel');
require('../public/javascripts/DateFormat');

var router = express.Router();


// mongoose.connect("mongodb://localhost/myBlog");
// var db = mongoose.connection;

mongoose.connection.openUri('mongodb://localhost/myBlog');


router.get('/', function (req, res, next) {
    res.render('add-article');
});


router.post('/add', function (req, res, next) {

    var query = req.body;

    query.time = new Date().Format("yyyy-MM-dd hh:mm:ss");

    articleModel.create(query, function (error) {
        if (!error) {
            console.log('save ok');

            res.redirect('../article');
        } else {
            console.log(error);
            res.write(error);
        }

        // db.close(function () {
        //     console.log('close');
        // });
    });
});


module.exports = router;
